<template>
  <div class="city">
    <div class="search_wrap">
      <div class="search">
        <i class="fa fa-search"></i>
        <input v-model="city_val" type="text" placeholder="输入城市名" />
      </div>
      <button
        @click="$router.push({ name: 'address', params: { city: city } })"
      >
        取消
      </button>
    </div>
    <div style="height: 100%" v-if="searchList.length == 0">
      <div class="localtion">
        <Localtion
          :address="city"
          v-if="
            this.$store.getters.localtion.addressComponent.city ||
            this.$store.getters.localtion.addressComponent.province
          "
          @click="selectCity(city)"
        ></Localtion>
      </div>
      <Alphabet
        :cityInfo="cityInfo"
        ref="allcity"
        @selectcity="selectCity"
      ></Alphabet>
    </div>
    <div class="search_list" v-else>
      <ul>
        <li
          @click="selectCity(item)"
          v-for="(item, index) in searchList"
          :key="index"
        >
          {{ item }}
        </li>
      </ul>
    </div>
    <Tabbar :data="tabbarData"></Tabbar>
  </div>
</template>
<script>
import Localtion from "../components/Localtion";
import Alphabet from "../components/Alphabet";
import Tabbar from "../components/Tabbar";
export default {
  name: "City",
  data() {
    return {
      city_val: "",
      cityInfo: null,
      tabbarData: [
        { title: "首页", icon: "home", path: "/home" },
        { title: "订单", icon: "file-text-o", path: "/order" },
        { title: "个人中心", icon: "user", path: "/me" },
      ],
      keys: [],
      allCities: [],
      searchList: [],
    };
  },
  components: {
    Localtion,
    Alphabet,
    Tabbar,
  },
  computed: {
    city() {
      return (
        this.$store.getters.localtion.addressComponent.city ||
        this.$store.getters.localtion.addressComponent.province
      );
    },
  },
  created() {
    this.getCityInfo();
  },
  methods: {
    getCityInfo() {
      this.$axios("/cityapi")
        .then((res) => {
          this.cityInfo = res.data;
          this.$nextTick(() => {
            this.$refs.allcity.initScroll();
          });
          //存储所有城市
          this.keys = Object.keys(res.data.alphabet);
          this.keys.forEach((key) => {
            this.cityInfo.cityList[key].cities.forEach((city) => {
              this.allCities.push(city.name);
            });
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    selectCity(city) {
      this.$router.push({ name: "address", params: { city: city } });
    },
    searchCity() {
      if (!this.city_val) {
        this.searchList = [];
      } else {
        this.searchList = this.allCities.filter((item) => {
          return item.indexOf(this.city_val) != -1;
        });
      }
    },
  },
  watch: {
    city_val() {
      this.searchCity();
    },
  },
};
</script>
<style scoped>
.city {
  width: 100%;
  height: 100%;
  overflow: auto;
  box-sizing: border-box;
  padding-top: 45px;
}
.search_wrap {
  position: fixed;
  top: 0;
  height: 45px;
  width: 100%;
  background: #fff;
  box-sizing: border-box;
  padding: 3px 16px;
  display: flex;
  justify-content: space-between;
}
.search {
  background-color: #eee;
  border-radius: 10px;
  line-height: 40px;
  width: 85%;
  box-sizing: border-box;
  padding: 0 16px;
}
.search input {
  background: #eee;
  outline: none;
  border: none;
  margin-left: 5px;
}
.search_wrap button {
  outline: none;
  border: none;
  background-color: #fff;
  color: #009eef;
}

.localtion {
  background: #fff;
  padding: 8px 16px;
  height: 65px;
  box-sizing: border-box;
}

.search_list {
  background: #fff;
  padding: 5px 16px;
}
.search_list li {
  padding: 10px;
  border-bottom: 1px solid #eee;
}
</style>